package com.bluetooth.heartchecked;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.bluetooth.heartchecked.adapter.HistoryAdapter;
import com.bluetooth.heartchecked.bean.HeartRateEntity;
import com.bluetooth.heartchecked.db.DBManager;
import com.bluetooth.heartchecked.util.DateUtil;
import com.bluetooth.heartchecked.util.ToastUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class QueryActivity extends AppCompatActivity implements View.OnClickListener {

    EditText editMin;
    EditText editMax;
    TextView tvMsg;
    ListView listView;
    HistoryAdapter adapter;

    final List<HeartRateEntity> entities = new ArrayList<>();

    public static void start(Context context) {
        Intent starter = new Intent(context, QueryActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query);

        initUI();
    }

    private void initUI() {
        TextView tvTopTitleCenter = findViewById(R.id.tvTopTitleCenter);
        tvTopTitleCenter.setText("历史记录");

        editMin = findViewById(R.id.edt_min);
        editMax = findViewById(R.id.edt_max);
        listView = findViewById(R.id.listView);
        tvMsg = findViewById(R.id.tv_msg);

        List<HeartRateEntity> all = DBManager.getInstance().findAll();
        if (all != null && !all.isEmpty()) {
            entities.addAll(all);
        }
        listView.setAdapter(adapter = new HistoryAdapter(this, entities));
        if (entities.isEmpty()) {
            tvMsg.setText("查询结果: 空");
        } else {
            tvMsg.setText("查询结果: 全部，总数 " + entities.size());
        }

        findViewById(R.id.tvTopTitleLeft).setOnClickListener(this);
        findViewById(R.id.btn_add).setOnClickListener(this);
        findViewById(R.id.btn_query).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvTopTitleLeft:
                onBackPressed();
                break;
            case R.id.btn_query:
                String minRateStr = editMin.getText().toString().trim();
                String maxRateStr = editMax.getText().toString().trim();
                int minRate = convert2Int(minRateStr);
                int maxRate = convert2Int(maxRateStr);
                if (minRate > maxRate) {
                    ToastUtil.show("输入最小心率不能大于最大心率");
                    return;
                }
                if (minRate == 0 &&  maxRate == 0) {
                    ToastUtil.show("请输入心率范围");
                    return;
                }
                List<HeartRateEntity> rateEntities = DBManager.getInstance().find(minRate, maxRate);
                entities.clear();
                entities.addAll(rateEntities);
                adapter.notifyDataSetChanged();

                tvMsg.setText("查询结果: [" + minRate + " --- " + maxRate + "] 总数 " + entities.size());
                break;
            case R.id.btn_add:
                mockData();
                break;
            default:
                break;
        }
    }

    /**
     * String转int
     */
    private int convert2Int(String value) {
        try {
            return Integer.valueOf(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    private void mockData() {
        for (int i = 0; i < 50; i++) {
            HeartRateEntity entity = new HeartRateEntity();
            entity.setTime(DateUtil.dateToString(new Date()));
            entity.setRate(new Random().nextInt(150));
            DBManager.getInstance().add(entity);
        }
        List<HeartRateEntity> all = DBManager.getInstance().findAll();
        if (all != null && !all.isEmpty()) {
            entities.addAll(all);
        }
        adapter.notifyDataSetChanged();
    }

}
